package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.system.domain.StuDataCertificate;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.StuDataCertificateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.StuCertificate;
import com.ruoyi.system.mapper.StuDataMapper;
import com.ruoyi.system.domain.StuData;
import com.ruoyi.system.service.IStuDataService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-01
 */
@Service
public class StuDataServiceImpl implements IStuDataService 
{
    @Autowired
    private StuDataMapper stuDataMapper;

    @Autowired
    private StuDataCertificateMapper stuDataCertificateMapper;

    /**
     * 查询学生信息
     *
     * @param stuId 学生信息主键
     * @return 学生信息
     */
    @Override
    public StuData selectStuDataByStuId(Long stuId)
    {
        return stuDataMapper.selectStuDataByStuId(stuId);
    }

    /**
     * 查询学生信息列表
     * 
     * @param stuData 学生信息
     * @return 学生信息
     */
    @DataScope(deptAlias = "u",userAlias = "u")
    @Override
    public List<StuData> selectStuDataList(StuData stuData)
    {
        return stuDataMapper.selectStuDataList(stuData);
    }

    /**
     * 新增学生信息
     * 
     * @param stuData 学生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStuData(StuData stuData)
    {
        int rows = stuDataMapper.insertStuData(stuData);
        insertStuCertificate(stuData);
        return rows;
    }

    /**
     * 修改学生信息
     * 
     * @param stuData 学生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStuData(StuData stuData)
    {
        //stuDataMapper.deleteStuCertificateByStuId(stuData.getStuId());
        insertStuCertificate(stuData);
        return stuDataMapper.updateStuData(stuData);
    }

    /**
     * 批量删除学生信息
     * 
     * @param stuIds 需要删除的学生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStuDataByStuIds(String stuIds)
    {
        return stuDataMapper.deleteStuDataByStuIds(Convert.toStrArray(stuIds));
    }

    /**
     * 删除学生信息信息
     * 
     * @param stuId 学生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStuDataByStuId(Long stuId)
    {
        //stuDataMapper.deleteStuCertificateByStuId(stuId);
        return stuDataMapper.deleteStuDataByStuId(stuId);
    }


    /**
     * 查询学生信息
     *
     * @param stuData:
     *              restrictQuery 查询限制
     *              stuId 学生信息主键
     * @return 学生信息
     */
    @Override
    public StuData selectStuCerByStuId(StuData stuData)
    {
        return stuDataMapper.selectStuCerByStuId(stuData);
    }

    /**
     * 查询学生信息列表
     *
     * @param stuData 学生信息
     * @return 学生信息集合
     */
    @Override
    public List<StuData> selectAllocatedList(StuData stuData) {
        return stuDataMapper.selectAllocatedList(stuData);
    }

    @Override
    public int deleteAuthStudentData(StuData stuData) {
        stuData.setCerId(Convert.toLong(""));
        return stuDataMapper.updateStuData(stuData);
    }

    @Override
    public int insertAuthCertificates(Long stuId, String cerIds) {
        Long[] cers = Convert.toLongArray(cerIds);
        // 新增用户与角色管理
        List<StuDataCertificate> list = new ArrayList<StuDataCertificate>();
        for (Long cerId : cers)
        {
            StuDataCertificate dc = new StuDataCertificate();
            dc.setCerId(cerId);
            dc.setStuId(stuId);
            list.add(dc);
        }
        return stuDataCertificateMapper.batchStuDataCertificate(list);
    }

    @Override
    public int deleteAuthCertificate(StuDataCertificate stuDataCertificate) {
        StuDataCertificate stuDataCertificate1;
        return stuDataCertificateMapper.deleteStuDataCertificateInfo(stuDataCertificate);
    }

    @Override
    public int deleteAuthCertificates(Long stuId, String cerIds) {
        return stuDataCertificateMapper.deleteStuDataCertificateInfos(stuId , Convert.toLongArray(cerIds));
    }

    /**
     * 新增资格证书信息
     * 
     * @param stuData 学生信息对象
     */
    public void insertStuCertificate(StuData stuData)
    {
        List<StuCertificate> stuCertificateList = stuData.getStuCertificateList();
        Long stuId = stuData.getStuId();
        if (StringUtils.isNotNull(stuCertificateList))
        {
            List<StuCertificate> list = new ArrayList<StuCertificate>();
            for (StuCertificate stuCertificate : stuCertificateList)
            {
                stuCertificate.setStuId(stuId);
                list.add(stuCertificate);
            }
            if (list.size() > 0)
            {
                stuDataMapper.batchStuCertificate(list);
            }
        }
    }
}
